package zjc.bookkeeping.homepage;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.tools.BusinessDialog;
import zjc.bookkeeping.tools.CustomAlertDialog;
import zjc.bookkeeping.tools.KeyboardDialog;
import zjc.bookkeeping.tools.MethodDialog;
import zjc.bookkeeping.tools.RemarksDialog;
import zjc.bookkeeping.tools.SelectClassDialog;
import zjc.bookkeeping.tools.TimeDialog;
import zjc.bookkeeping.tools.ToolsClass;

public class ShowConsumption extends AppCompatActivity implements View.OnClickListener {
    ImageView backIv,deleteIv;
    TextView consumptionClassTv,moneyTv,accountTv,timeTv,methodTv,businessTv,remarksTv;
    TextView tvMethod,tvBusiness;
    LinearLayout moneyLl;
    Button editBtn,sureBtn;
    int accountId = DBManager.selectAccountId();
    ConsumptionBean consumptionBean;
    boolean editFlag = false;
    String kindName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_consumption);
        getConsumptionData();
        initView();
        controlStyle();
    }

    private void controlStyle() {
        switch (consumptionBean.getConsumption_kind()){
            case 1:
                tvMethod.setText("收入账户");
                tvBusiness.setVisibility(View.GONE);
                businessTv.setVisibility(View.GONE);
                break;
            case 2:
                tvMethod.setText("转出账户");
                tvBusiness.setText("转入账户");
        }
    }

    private void getConsumptionData() {
        consumptionBean = getIntent().getParcelableExtra("consumptionBean");
        switch (consumptionBean.getConsumption_kind()){
            case 0:
                kindName = "支出";
                break;
            case 1:
                kindName = "收入";
                break;
            case 2:
                kindName = "转账";
                break;
        }
        Log.i("1kindName",kindName);
    }

    private void initView() {
        backIv =findViewById(R.id.show_consumption_iv_back);
        deleteIv = findViewById(R.id.show_consumption_iv_delete);

        consumptionClassTv = findViewById(R.id.show_consumption_tv_class);
        consumptionClassTv.setText(consumptionBean.getConsumption_class_name());

        moneyTv = findViewById(R.id.show_consumption_tv_money);
        moneyTv.setText(String.valueOf(consumptionBean.getConsumption_money()));

        accountTv = findViewById(R.id.show_consumption_tv_account);
        accountTv.setText(DBManager.getAccountName(accountId));

        timeTv = findViewById(R.id.show_consumption_tv_time);
        timeTv.setText(consumptionBean.getConsumption_time());

        methodTv = findViewById(R.id.show_consumption_tv_method);
        methodTv.setText(consumptionBean.getConsumption_method());
        tvMethod = findViewById(R.id.show_tv_method);

        businessTv = findViewById(R.id.show_consumption_tv_business);
        businessTv.setText(consumptionBean.getConsumption_business());
        tvBusiness = findViewById(R.id.show_tv_business);

        remarksTv = findViewById(R.id.show_consumption_tv_remarks);
        remarksTv.setText(consumptionBean.getConsumption_remarks());

        moneyLl = findViewById(R.id.show_consumption_ll_money);

        editBtn = findViewById(R.id.show_consumption_btn_edit);
        sureBtn = findViewById(R.id.show_consumption_btn_sure);

        backIv.setOnClickListener(this);
        deleteIv.setOnClickListener(this);
        consumptionClassTv.setOnClickListener(this);
        moneyLl.setOnClickListener(this);
        accountTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
        methodTv.setOnClickListener(this);
        businessTv.setOnClickListener(this);
        remarksTv.setOnClickListener(this);
        editBtn.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.show_consumption_iv_back:
                finish();
                break;
            case R.id.show_consumption_iv_delete:
                showAlertDialog();
                break;
            case R.id.show_consumption_btn_edit:
                if (editFlag){
                    editFlag = false;
                    editBtn.setText("编辑");
                    initNoEditColor();
                }else {
                    editFlag = true;
                    editBtn.setText("取消编辑");
                    initEditColor();
                }
                break;
            case R.id.show_consumption_tv_class:
                if (editFlag && consumptionBean.getConsumption_kind() != 2){
                    showClassDialog();
                }
                break;
            case R.id.show_consumption_ll_money:
                if (editFlag){
                    showMoneyDialog();
                }
                break;
            case R.id.show_consumption_tv_account:
                if (editFlag){
                    showAccountDialog();
                }
                break;
            case R.id.show_consumption_tv_time:
                if (editFlag){
                    showTimeDialog();
                }
                break;
            case R.id.show_consumption_tv_method:
                if (editFlag){
                    showMethodDialog();
                }
                break;
            case R.id.show_consumption_tv_business:
                if (editFlag){
                    showBusinessDialog();
                }
                break;
            case R.id.show_consumption_tv_remarks:
                if (editFlag){
                    showRemarksDialog();
                }
                break;
            case R.id.show_consumption_btn_sure:
                if (consumptionBean.getConsumption_money() == 0){
                    ToolsClass.showToast(ShowConsumption.this,"请输入金额，且不能为0.", Toast.LENGTH_SHORT);
                }else {
                    if (consumptionBean.getConsumption_method().equals(consumptionBean.getConsumption_business())){
                        ToolsClass.showToast(ShowConsumption.this,"账户不能相同。",Toast.LENGTH_SHORT);
                    }else {
                        DBManager.updateConsumption(consumptionBean);
                        finish();
                    }
                }
                break;
        }
    }

    private void showAlertDialog() {
        CustomAlertDialog customAlertDialog = new CustomAlertDialog(this);
        customAlertDialog.show();
        customAlertDialog.getConsumptionId(consumptionBean.getConsumption_id());
        customAlertDialog.setOnSureListener(new CustomAlertDialog.OnSureListener(){
            @Override
            public void onSure() {
                ToolsClass.showToast(ShowConsumption.this,"账单记录已删除。", Toast.LENGTH_SHORT);
                finish();
            }
        });
    }

    private void showRemarksDialog() {
        RemarksDialog remarksDialog = new RemarksDialog(this);
        remarksDialog.show();
        String str = remarksTv.getText().toString();
        remarksDialog.setRemarksEt(str);
        remarksDialog.setRemarksDialogSize();
        remarksDialog.setOnRemarksSureListener(new RemarksDialog.OnRemarksSureListener() {
            @Override
            public void onRemarksSure() {
                String str = remarksDialog.getRemarksEt();
                if (!TextUtils.isEmpty(str)){
                    remarksTv.setText(str);
                    consumptionBean.setConsumption_remarks(str);
                }
            }
        });
    }

    private void showBusinessDialog() {
        if (consumptionBean.getConsumption_kind() == 0){
            BusinessDialog businessDialog = new BusinessDialog(this);
            businessDialog.show();
            businessDialog.setOnBusinessItemListener(new BusinessDialog.OnBusinessItemListener() {
                @Override
                public void onBusinessItemClick() {
                    String str = businessDialog.getBusiness();
                    businessTv.setText(str);
                    consumptionBean.setConsumption_business(str);
                }
            });
        }else {
            MethodDialog methodDialog = new MethodDialog(this);
            methodDialog.show();
            methodDialog.setOnMethodItemListener(new MethodDialog.OnMethodItemListener() {
                @Override
                public void onMethodItemClick() {
                    String str = methodDialog.getMethod();
                    businessTv.setText(str);
                    consumptionBean.setConsumption_business(str);
                }
            });
        }
    }

    private void showMethodDialog() {
        MethodDialog methodDialog = new MethodDialog(this);
        methodDialog.show();
        methodDialog.setOnMethodItemListener(new MethodDialog.OnMethodItemListener() {
            @Override
            public void onMethodItemClick() {
                String str = methodDialog.getMethod();
                methodTv.setText(str);
                consumptionBean.setConsumption_method(str);
            }
        });
    }

    private void showTimeDialog() {
        TimeDialog timeDialog = new TimeDialog(this);
        timeDialog.show();
        timeDialog.setOnTimeSureListener(new TimeDialog.OnTimeSureListener() {
            @Override
            public void onTimeSure(String time, int year, int month, int day) {
                timeTv.setText(time);
                consumptionBean.setConsumption_time(time);
                consumptionBean.setConsumption_year(year);
                consumptionBean.setConsumption_month(month);
                consumptionBean.setConsumption_day(day);
            }
        });
    }

    private void showAccountDialog() {
        SelectClassDialog selectClassDialog = new SelectClassDialog(this);
        selectClassDialog.show();
        selectClassDialog.setClassDialogSize();
        selectClassDialog.showNameList("账本");
        selectClassDialog.setOnSelectClassListener(new SelectClassDialog.OnSelectClassListener() {
            @Override
            public void onSelectClassSure(String name) {
                accountTv.setText(name);
            }
        });
    }

    private void showClassDialog() {
        SelectClassDialog selectClassDialog = new SelectClassDialog(this);
        selectClassDialog.show();
        selectClassDialog.setClassDialogSize();
        Log.i("kindName",kindName);
        selectClassDialog.showNameList(kindName);
        selectClassDialog.setOnSelectClassListener(new SelectClassDialog.OnSelectClassListener() {
            @Override
            public void onSelectClassSure(String name) {
                consumptionClassTv.setText(name);
                consumptionBean.setConsumption_class_name(name);
            }
        });
    }

    private void showMoneyDialog() {
        KeyboardDialog keyboardDialog = new KeyboardDialog(this);
        keyboardDialog.show();
        keyboardDialog.setKeyboardDialogSize();
        keyboardDialog.setOnKeyboardSureListener(new KeyboardDialog.OnKeyboardSureListener() {
            @Override
            public void onKeyboardSure() {
                String str = keyboardDialog.getMoneyTv();
                if (!TextUtils.isEmpty(str)){
                    moneyTv.setText(str);
                    float money = Float.parseFloat(str);
                    consumptionBean.setConsumption_money(money);
                }
            }
        });
    }

    private void initEditColor() {
        if (consumptionBean.getConsumption_kind() != 2){
            consumptionClassTv.setBackgroundResource(R.drawable.home_spinner_background);
        }
        moneyLl.setBackgroundResource(R.drawable.home_spinner_background);
        accountTv.setBackgroundResource(R.drawable.home_spinner_background);
        timeTv.setBackgroundResource(R.drawable.home_spinner_background);
        methodTv.setBackgroundResource(R.drawable.home_spinner_background);
        businessTv.setBackgroundResource(R.drawable.home_spinner_background);
        remarksTv.setBackgroundResource(R.drawable.home_spinner_background);
    }

    private void initNoEditColor() {
        consumptionClassTv.setBackgroundResource(R.drawable.show_consumption_background);
        moneyLl.setBackgroundResource(R.drawable.show_consumption_background);
        accountTv.setBackgroundResource(R.drawable.show_consumption_background);
        timeTv.setBackgroundResource(R.drawable.show_consumption_background);
        methodTv.setBackgroundResource(R.drawable.show_consumption_background);
        businessTv.setBackgroundResource(R.drawable.show_consumption_background);
        remarksTv.setBackgroundResource(R.drawable.show_consumption_background);
    }
}